package com.csxy.six.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.six.R2;
import com.csxy.six.entity.User;
import com.csxy.six.exception.MyException;
import com.csxy.six.mapper.UserMapper;
import com.csxy.six.service.UserService;
import com.csxy.six.util.SecurityUtil; // 需要创建这个工具类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public R2<List<User>> listUsers(String username, String nickname, Integer status) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (username != null) wrapper.like("username", username);
        if (nickname != null) wrapper.like("nickname", nickname);
        if (status != null) wrapper.eq("status", status);
        return R2.ok(userMapper.selectList(wrapper));
    }

    @Override
    public R2<User> getUserById(Long id) {
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new MyException(404, "用户不存在");
        }
        return R2.ok(user);
    }

    @Override
    public R2<User> createUser(User user) {
        userMapper.insert(user);
        return R2.ok(user);
    }

    @Override
    public R2<User> updateUser(Long id, User user) {
        User existingUser = userMapper.selectById(id);
        if (existingUser == null) {
            throw new MyException(404, "用户不存在");
        }
        user.setId(id);
        userMapper.updateById(user);
        return R2.ok(user);
    }

    @Override
    public R2<Void> deleteUser(Long id) {
        if (userMapper.deleteById(id) > 0) {
            return R2.ok();
        }
        throw new MyException(404, "用户不存在");
    }

    @Override
    public R2<Void> updateUserStatus(Long id, Integer status) {
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new MyException(404, "用户不存在");
        }
        user.setStatus(status);
        userMapper.updateById(user);
        return R2.ok();
    }

    @Override
    public R2<Void> changePassword(String oldPassword, String newPassword) {
        // 实现修改密码逻辑
        return R2.ok();
    }

    @Override
    public void updateAvatar(Long userId, String avatarUrl) {
        User user = userMapper.selectById(userId);
        if (user != null) {
            user.setAvatar(avatarUrl);
            userMapper.updateById(user);
        }
    }

    @Override
    public R2<User> getCurrentUserProfile() {
        // 获取当前登录用户的ID
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            throw new MyException(401, "用户未登录");
        }

        // 查询用户信息
        User user = userMapper.selectById(currentUserId);
        if (user == null) {
            throw new MyException(404, "用户不存在");
        }

        // 返回用户信息（注意：应排除敏感信息如密码）
        user.setPassword(null); // 清除密码
        return R2.ok(user);
    }

    @Override
    public R2<User> updateCurrentUserProfile(User user) {
        // 获取当前登录用户的ID
        Long currentUserId = SecurityUtil.getCurrentUserId();
        if (currentUserId == null) {
            throw new MyException(401, "用户未登录");
        }

        // 确保更新的是当前用户自己的资料
        user.setId(currentUserId);

        // 更新用户信息（注意：应排除不能修改的字段如密码）
        user.setPassword(null); // 密码不能通过此接口修改
        userMapper.updateById(user);

        // 返回更新后的用户信息
        User updatedUser = userMapper.selectById(currentUserId);
        updatedUser.setPassword(null); // 清除密码
        return R2.ok(updatedUser);
    }
}